package com.uphone.magicleafteacher.pro.activity.mine;

import android.arch.lifecycle.LifecycleOwner;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.radish.baselibrary.utils.ToastUtil;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.BaseGActivity;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.FailureConsumer;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.http.SuccessfulConsumer;
import com.uphone.magicleafteacher.util.CommonUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class MainpasdActivity extends BaseGActivity {


    @BindView(R.id.img_return)
    ImageView imgReturn;
    @BindView(R.id.button_affirm)
    Button buttonAffirm;
    @BindView(R.id.edit_raw)
    EditText editRaw;
    @BindView(R.id.edit_new)
    EditText editNew;
    @BindView(R.id.edit_affirm)
    EditText editAffirm;

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_mainpasd;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }


    @OnClick({R.id.img_return, R.id.button_affirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_return:
                finish();
                break;
            //提交修改密码
            case R.id.button_affirm:
                changePassword();
                break;
        }
    }

    private void changePassword() {
        if (CommonUtil.checkViewEmpty(new String[]{"请输入原密码", "请输入新密码", "请输入确认密码"}, editRaw, editNew, editAffirm))
            return;

        if (!TextUtils.equals(editNew.getText().toString(), editAffirm.getText().toString())) {
            ToastUtil.showShort("两次密码不一致");
            return;
        }
        String oldpassword = editRaw.getText().toString();
        String newpassword = editNew.getText().toString();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("teacherId", CommonUtil.getTeacherId());
        map1.put("oldPassword", oldpassword);
        map1.put("newPassword", newpassword);
        MyApp.apiService(ApiService.class)
                .change(map1)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    showLoading();
                })
                .doFinally(() -> {
                    closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) this)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        CommonUtil.quitActivity(MainpasdActivity.this, "修改密码成功");
                    }
                }, new FailureConsumer());

    }
}
